package com.endoc.livedatabuslearn;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;

import android.os.Bundle;
import android.util.Log;

import com.jeremyliao.liveeventbus.LiveEventBus;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        final LiveDataViewModel instance = LiveDataViewModel.getInstance(this);

        LiveEventBus
                //TODO 此处key就是用来作为订阅的唯一标识
                .get("String.class_key", String.class)
                .observeSticky(this, new Observer<String>() {
                    @Override
                    public void onChanged(@Nullable String s) {
                        Log.d(StringDataUitls.LiveDataBus,"Main2Activity:   "+s);
                    }
                });



        Log.d(StringDataUitls.LiveDataBus,"MainActivity2:    "+String.valueOf(instance));
        LiveEventBus
                //todo 此处key就是用来作为订阅的唯一标识,可以设计为ViewModel的地址,同一个viewModel的地址肯定一致,所以能实现viewModel和里面的数据绑定
                .get(String.valueOf(instance),Person.class)
                .observeSticky(this, new Observer<Person>() {
                    @Override
                    public void onChanged(Person person) {
                        Log.d(StringDataUitls.LiveDataBus,"Main2Activity:   "+person.name);
                    }
                });





    }
}
